package com.zt.english.activity.main

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import androidx.core.view.get
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.gyf.immersionbar.ImmersionBar
import com.zt.english.R
import com.zt.english.adapter.MainVpAdapter
import com.zt.english.databinding.ActivityMainBinding
import com.zt.mvvm.view.BaseActivity
import com.zt.mvvm.view.BaseLifeCycleActivity

class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
    override fun initView() {
        super.initView()
        initVpBottomNav()
    }
    fun initVpBottomNav(){
        mDataBinding.bottomNavView.itemIconTintList = null //取消icon的默认着色
        mDataBinding.vp.adapter =  MainVpAdapter(this)
        var barDarkFont = true //状态栏字体是否深色
        mDataBinding.vp.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                var fragment_id: Int = R.id.navigation_word
                when (position) {
                    0 -> {
                        fragment_id = R.id.navigation_word
                        barDarkFont = true
                    }
                    1 -> {
                        fragment_id = R.id.navigation_discover
                        barDarkFont = true
                    }
                    2 -> {
                        fragment_id = R.id.navigation_listen
                        barDarkFont = true
                    }
                }
                mDataBinding.bottomNavView.selectedItemId = fragment_id
                ImmersionBar.with(this@MainActivity).statusBarDarkFont(barDarkFont).init()
            }
        })
        val smoothScroll = true //点击tab,vp切换是否显示滑动效果
        mDataBinding.bottomNavView.setOnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.navigation_word -> {
                    mDataBinding.vp.setCurrentItem(0, smoothScroll)
                    barDarkFont = true
                }
                R.id.navigation_discover -> {
                    mDataBinding.vp.setCurrentItem(1, smoothScroll)
                    barDarkFont = true
                }
                R.id.navigation_listen ->{
                    mDataBinding.vp.setCurrentItem(2, smoothScroll)
                    barDarkFont = true
                }
            }
            ImmersionBar.with(this).statusBarDarkFont(barDarkFont).init()
            true
        }
    }

    override fun darkFont(): Boolean = true

    override fun enterActivityAnim(startAnim: Int, exitAnim: Int) {
        super.enterActivityAnim(R.anim.anim_scale, R.anim.translate_none)
    }

    override fun initImmersionBar() {
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .navigationBarColor(R.color.color_navigation)
                .init()
    }

    override fun isSupportSwipeBack(): Boolean = false

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(false)
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}